<?php

namespace app\api\controller;

use think\Controller;

class GameTask extends Controller
{

    public function __construct()
    {
        parent::__construct();
        set_cors();
    }

    public function duoyou(){
        // die;
        $get = request()->get();

        if(empty($get['user_id'])){
            return 'empty';
        }

        $TotalLottery = db('accounts_info','db_config2')->where('UserID','=',$get['user_id'])->value('ByLottery');
        $lottery = db('activity_duoyou','db_operatelog')->where('UserID','=',$get['user_id'])->find();
        if($lottery){
            // 每次更新new_lottery
            db('activity_duoyou','db_operatelog')->where('UserID','=',$get['user_id'])->setField('new_lottery',$TotalLottery);
        }else{
            $int = db('activity_duoyou','db_operatelog')->insert(
                [
                    'UserID'=>$get['user_id'],
                    'add_time'=>date('Y-m-d H:i:s'),
                    'old_lottery'=>$TotalLottery,
                    'new_lottery'=>$TotalLottery,
                ]
            );
        }

        return 'ok';
    }

    public function duoyou_roleinfo(){
        $get = request()->get();
        if(empty($get['user_id']) || empty($get['sign'])){
            return 'empty';
        }

        $sign = md5('user_id='.$get['user_id'].'&key=jincanbuyu&Duoyou202005666');
        if($sign != $get['sign']){
            return json_encode(['state_code'=>400,'msg'=>'签名错误'],JSON_UNESCAPED_UNICODE);
        }

        $user = db('accounts_info','db_config2')
            ->field('UserID,NickName,VipLevel,Score,Lottery,LastLoginDate')
            ->where('UserID','=',$get['user_id'])->find();
        if(empty($user)){
            return json_encode(['state_code'=>300,'msg'=>'角色ID不存在']);
        }

        $data['state_code'] = 200;
        $data['role_info']['role_id'] = $user['UserID'];
        $data['role_info']['role_name'] = $user['NickName'];
        $data['role_info']['role_vip'] = $user['VipLevel'];
        $data['role_info']['role_gold'] = $user['Score'];
        $data['role_info']['role_huafeiquan'] = $user['Lottery'];
        $data['role_info']['update_time'] = $user['LastLoginDate'];

        $phone = db('user_bind_info','db_config2')
            ->where('UserID','=',$get['user_id'])->value('Phone');

        $data['role_info']['role_mobile'] = empty($phone)?0:$phone;

        $lottery = db('activity_duoyou','db_operatelog')->where('UserID','=',$get['user_id'])->find();

        $data['role_data']['role_get_yuquan'] = $lottery['new_lottery'] - $lottery['old_lottery'];

        return json_encode($data);
    }

    //--------------------------------------------------红彤彤 begin-------------------------//
    public function hongtongtong(){
    
        $get = request()->get();
        if(empty($get['user_id'])){
            return 'empty';
        }

        $accounts_info = db('accounts_info','db_config2')->where('UserID','=',$get['user_id'])->find();

        $activity_version = db('activity_version','db_operatelog')->where(['name'=>'hongtongtong'])->find();
        $activity_hongtongtong = db('activity_hongtongtong','db_operatelog')->where(['version'=>$activity_version['version'],'UserID'=>$get['user_id']])->find();
        
        if($activity_hongtongtong){
            // 每次更新new_lottery
            db('activity_hongtongtong','db_operatelog')->where('UserID','=',$get['user_id'])->setField('new_lottery',$accounts_info['ByLottery']);
        }else{
            //获取第三方游戏id
            $game_user_id = 0;
            if($accounts_info['SpreaderID'] == '10057'){
                $a = explode('kwy_10057_',$accounts_info['UniqueID']);
                $game_user_id = empty($a[1])?0:$a[1];
            }
            if($accounts_info['SpreaderID'] == '783'){
                $a = explode('freegame_',$accounts_info['UniqueID']);
                $game_user_id = empty($a[1])?0:$a[1];
            }

            db('activity_hongtongtong','db_operatelog')->insert(
                [
                    'UserID'=>$get['user_id'],
                    'add_time'=>date('Y-m-d H:i:s'),
                    'old_lottery'=>$accounts_info['ByLottery'],
                    'new_lottery'=>$accounts_info['ByLottery'],
                    'version'=>$activity_version['version'],
                    'channel_id'=>$accounts_info['SpreaderID'],
                    'game_user_id'=>$game_user_id,
                ]
            );
        }
        return 'ok';
    }
    
    public function hongtongtong_roleinfo(){

        $get = request()->get();
        mylog($get);
        if(empty($get['user_id']) || empty($get['sign']) || empty($get['start_time'])|| empty($get['end_time']) || empty($get['channel_id'])){
            exit(json_encode(['code'=>300,'msg'=>'缺少参数']));
        }

        $sign = md5($get['user_id'].'jincanbuyu&hongtongtong');
        if($sign != $get['sign']){
            exit(json_encode(['code'=>300,'msg'=>'签名错误']));
        }
       
        // $user = db('accounts_info','db_config2')->where(['UniqueID'=>$get['user_id'],'SpreaderID'=>$get['channel_id']])->find();
        // if(empty($user)){
        //     exit(json_encode(['code'=>300,'msg'=>'未找到该游戏用户']));
        // }
       
        $activity_version = db('activity_version','db_operatelog')->where(['name'=>'hongtongtong'])->find();//手动改version 来控制时间查询不用删activity_hongtongtong数据

        $activity_hongtongtong = db('activity_hongtongtong','db_operatelog')->where([
            'version'=>$activity_version['version'],
            'game_user_id'=>$get['user_id'],
            'channel_id'=>$get['channel_id']
        ])
        ->where('add_time','>=',$get['start_time'])
        ->where('add_time','<=',$get['end_time'])
        ->find();
        if(!$activity_hongtongtong){
            exit(json_encode(['code'=>300,'msg'=>'条件中未找到该用户鱼券数据']));
        }

        $role_get_yuquan = $activity_hongtongtong['new_lottery'] - $activity_hongtongtong['old_lottery'];

        $user = db('accounts_info','db_config2')->where(['UserID'=>$activity_hongtongtong['UserID']])->find();
        $data = [
            'code'=>200,
            'msg'=>'',
            'data'=>[
                'user_id'=>$user['UserID'],
                'name'=>$user['NickName'],
                'vip'=>$user['VipLevel'],
                'score'=>$user['Score'],
                'lottery'=>$user['Lottery'],
                'last_login_date'=>$user['LastLoginDate'],
                'role_get_yuquan'=>$role_get_yuquan
            ]
        ];

        exit(json_encode($data));
    }

    //--------------------------------------------------红彤彤 end-------------------------//

}

